<template>
   <div class="app-container">
      <div class="p4">
         <FaSearchBar :show-toggle="false">
            <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="100px">
               <el-form-item label="登录地址" prop="ip">
                  <el-input v-model="queryParams.ip" placeholder="请输入登录地址" clearable style="width: 200px"
                     @keyup.enter="handleQuery" />
               </el-form-item>
               <el-form-item label="用户名称" prop="username">
                  <el-input v-model="queryParams.username" placeholder="请输入用户名称" clearable style="width: 200px"
                     @keyup.enter="handleQuery" />
               </el-form-item>
               <el-form-item style="margin-left: 100px;">
                  <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                  <el-button icon="Refresh" @click="resetQuery">重置</el-button>
               </el-form-item>
            </el-form>
         </FaSearchBar>
         <el-divider border-style="dashed" style="margin:0px 0px 20px 0px" />

         <el-table v-loading="loading" :data="onlineList.slice((pageNum - 1) * pageSize, pageNum * pageSize)"
            style="width: 100%;" stripe highlight-current-row border class="mt4">
            <el-table-column label="序号" width="60" type="index" align="center" />
            <el-table-column label="会话编号" align="center" prop="accessToken" :show-overflow-tooltip="true" />
            <el-table-column label="登录用户名" align="center" prop="username" :show-overflow-tooltip="true" />
            <el-table-column label="IP" align="center" prop="ip" :show-overflow-tooltip="true" />
            <el-table-column label="登录地点" align="center" prop="location" :show-overflow-tooltip="true" />
            <el-table-column label="操作系统" align="center" prop="os" :show-overflow-tooltip="true" />
            <el-table-column label="浏览器" align="center" prop="browser" :show-overflow-tooltip="true" />
            <el-table-column label="登录时间" align="center" prop="loginAt" width="180">
               <template #default="scope">
                  <span>{{ parseTime(scope.row.loginAt) }}</span>
               </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
               <template #default="scope">
                  <el-button size="small" plain type="danger" icon="Delete" @click="handleForceLogout(scope.row)"
                     v-auth="['system:online$forceLogout']">强退</el-button>
               </template>
            </el-table-column>
         </el-table>

         <pagination v-show="total > 0" :total="total" v-model:page="pageNum" v-model:limit="pageSize" />
      </div>
   </div>
</template>

<script setup name="Online">
import apiOnline from "@/api/monitor/online";

const { proxy } = getCurrentInstance();

const onlineList = ref([]);
const loading = ref(false);
const total = ref(0);
const pageNum = ref(1);
const pageSize = ref(10);

const queryParams = ref({
   ip: undefined,
   username: undefined
});

/** 查询登录日志列表 */
function getList() {
   // loading.value = true;
   apiOnline.list(queryParams.value).then(response => {
      onlineList.value = response.data.rows;
      total.value = response.data.total;
   }).finally(() => {
    loading.value = false
  })
}
/** 搜索按钮操作 */
function handleQuery() {
   pageNum.value = 1;
   getList();
}
/** 重置按钮操作 */
function resetQuery() {
   proxy.resetForm("queryRef");
   handleQuery();
}
/** 强退按钮操作 */
function handleForceLogout(row) {
   proxy.$modal.confirm('是否确认强退名称为"' + row.username + '"的用户?').then(function () {
      return apiOnline.forceLogout(row.accessToken);
   }).then(() => {
      getList();
      proxy.$modal.success("强退成功");
   }).catch(() => { });
}

getList();
</script>
